//
//  HomePageController.m
//  testTry
//
//  Created by apple on 2017/2/26.
//  Copyright © 2017年 CollectionView. All rights reserved.
//

#import "HomePageController.h"
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface HomePageController () <CLLocationManagerDelegate >
@property (nonatomic, strong) CLLocationManager* locationManage;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,weak)UIButton *button;
@property (nonatomic,strong)AVPlayer *player;

@end

@implementation HomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.city = @"定位中..";
    [self locateButton];
    
    
    [self locate];
    
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    
    NSString *string = @"测试";
    
    [pab setString:string];
    
//    if (pab == nil) {
//        [MBProgressHUD showError:@"复制失败"];
//        
//    }else
//    {
//        [MBProgressHUD showSuccess:@"已复制"];
//    }
    NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
    
    // 2.创建AVPlayerItem
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    
    // 3.创建AVPlayer
    _player = [AVPlayer playerWithPlayerItem:item];
    
    // 4.添加AVPlayerLayer
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    layer.frame = CGRectMake(0, 200, self.view.frame.size.width, 200);
    layer.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.view.layer addSublayer:layer];
    [self.player play];
    
}

//-(AVPlayer *)avPlayer{
//    if (_player == nil) {
//        // 1.获取URL(远程/本地)
//        // NSURL *url = [[NSBundle mainBundle] URLForResource:@"01-知识回顾.mp4" withExtension:nil];
//        NSURL *url = [NSURL URLWithString:@"ftp://127.0.0.1/1.mp4"];
//        
//        // 2.创建AVPlayerItem
//        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
//        
//        // 3.创建AVPlayer
//        _player = [AVPlayer playerWithPlayerItem:item];
//        
//        // 4.添加AVPlayerLayer
//        AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
//        layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
//        [self.view.layer addSublayer:layer];
//    }
//    return _player;
//}

-(void)acivityBtnClick :(UIButton *)button{
    [self locate];
 }

-(void)locate{
    if([CLLocationManager locationServicesEnabled]) {
        //定位初始化
        _locationManage=[[CLLocationManager alloc] init];
        _locationManage.delegate=self;
        _locationManage.desiredAccuracy=kCLLocationAccuracyBest;
        _locationManage.distanceFilter=10;
            [self.locationManage requestWhenInUseAuthorization];

        [_locationManage startUpdatingLocation];//开启定位
    }else {
        //提示用户无法进行定位操作
        UIAlertController *alert= [UIAlertController alertControllerWithTitle:@"提示" message:@"定位不成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alert animated:YES completion:nil];
        }
    [_locationManage startUpdatingLocation];

    
}

//代理设置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //此处locations存储了持续更新的位置坐标值，取最后一个值为最新位置，如果不想让其持续更新位置，则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
    CLLocation *currentLocation = [locations lastObject];
    // 获取当前所在的城市名
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"locate" object:nil userInfo:@{@"location":currentLocation}];
  
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             
             
             //NSLog(@%@,placemark.name);//具体位置
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                 city = placemark.administrativeArea;
             }
             NSLog(@"定位完成:%@",city);
             [self.button setTitle:city forState:UIControlStateNormal];
             
             
             
             //系统会一直更新数据，直到选择停止更新，因为我们只需要获得一次经纬度即可，所以获取之后就停止更新
             [manager stopUpdatingLocation];
         }else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
}

//- (void)findMe
//{
//    /** 由于IOS8中定位的授权机制改变 需要进行手动授权
//     * 获取授权认证，两个方法：
//     [self.locationManager requestWhenInUseAuthorization];
//    [self.locationManager requestAlwaysAuthorization];
//     */
//    
//    [self.locationManager requestWhenInUseAuthorization];
//    [self.locationManager requestAlwaysAuthorization];
//
//    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//        NSLog(@"requestAlwaysAuthorization");
//        [self.locationManager requestAlwaysAuthorization];
//    }
//    
//    //开始定位，不断调用其代理方法
//    [self.locationManager startUpdatingLocation];
//    NSLog(@"start gps");
//    
//}
//


-(void)locateButton{
    //设置定位button按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button = btn;
    btn.frame = CGRectMake(50, 50, 80, 40);
    [btn setImage:[UIImage imageNamed:@"icon_mapmarker_cir"] forState:UIControlStateNormal];
    [btn setTitle:self.city forState:UIControlStateNormal];
    
    btn.titleLabel.font= [UIFont systemFontOfSize:12 ];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, btn.frame.size.width - btn.imageView.frame.origin.x - btn.imageView.frame.size.width);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, - btn.imageView.frame.size.width +20, 0, 0);
    
    //    [btn setImage:[UIImage imageNamed :@"icon_mapmarker_cir"] forState:UIControlStateNormal];
    btn.adjustsImageWhenHighlighted = NO;
    btn.showsTouchWhenHighlighted = YES;
    [btn sizeToFit]; // 调整大小
    
    [btn addTarget:self action:@selector(acivityBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 2.需要将按钮保证成item
    UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.leftBarButtonItem = activityItem;
    
}


@end
